package com.timoon.sample.web.tree;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.tree.Tree;
import org.apache.wicket.markup.html.panel.Panel;

import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class TreePanel extends Panel {

	public TreePanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		final JQueryFeedbackPanel feedback=new JQueryFeedbackPanel("wk_feedback");
		add(feedback);
		
		Tree tree=new Tree("wk_tree",this.getTreeModel()){
			@Override
			protected void onNodeLinkClicked(AjaxRequestTarget target,
					TreeNode node) {
				// TODO Auto-generated method stub
				super.onNodeLinkClicked(target, node);
				String text=(String) ((DefaultMutableTreeNode)node).getUserObject();
				this.error("您单击了节点>>"+text);
				target.add(feedback);
			}
		};
		add(tree);
	}
	
	/**
	 * 组装数据
	 * */
	private DefaultTreeModel getTreeModel(){
		
		DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
		
		for(int i=0;i<5;i++){
			DefaultMutableTreeNode parent=new DefaultMutableTreeNode("parent-"+i);
			root.add(parent);
			for(int j=0;j<5;j++){
				DefaultMutableTreeNode child=new DefaultMutableTreeNode("child-"+j);
				parent.add(child);
			}
		}
		
		return new DefaultTreeModel(root);
	}

}
